package com.yjs.app.trade.web.controller.business.statistics;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjs.app.trade.core.util.DateUtil;
import com.yjs.app.trade.core.util.MongoUtil;
import com.yjs.app.trade.core.util.StringUtils;
import com.yjs.app.trade.entity.base.StatisticsConstant;
import com.yjs.app.trade.web.controller.base.BaseController;
import com.yjs.app.trade.web.controller.base.util.Result;

import net.sf.json.JSONObject;

/**
 * 发票相关控制层
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/app/business/statistics/union")
public class UnionStatisticsController extends BaseController {
	
	/**
	 * 获取医疗机构30天内每天的订单、支付、收货金额
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/unionOrderPriceInfoThirtyDays", method = RequestMethod.GET)
	@ResponseBody
	public Object unionOrderPriceInfoThirtyDays(String data, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("unionId", getUserMemberId(request));
		List<String> projection = new ArrayList<String>();
		projection.add("unionId");
		Map<String, Integer> sorts = new HashMap<String, Integer>();
		sorts.put("created", 1);
		List<String> list = MongoUtil.getListInfo(StatisticsConstant.UnionCollection.UNIONORDERPRICEINFOTHIRTYDAYS, map, projection, null, null, sorts);
		List<String> dates = DateUtil.getLastDates(new Date(), 30, Calendar.DATE, DateUtil.dateFormat);
		boolean flag = false;
		for (String string : dates) {
			flag = false;
			for (String json : list) {
				JSONObject jsonObject = JSONObject.fromObject(json);
				if (jsonObject.get("created") != null) {
					if (StringUtils.equals(string, jsonObject.getString("created"))) {
						flag = true;
					}
				}
			}
			if (!flag) {
				list.add("{\"created\":\"" + string + "\"}");
			}
		}
		list.sort(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				JSONObject jsonObject1 = JSONObject.fromObject(o1);
				JSONObject jsonObject2 = JSONObject.fromObject(o2);
				if (jsonObject1.get("created") != null && jsonObject2.get("created") != null ) {
					try {
						Date date1 = DateUtil.dateFormat.parse(jsonObject1.getString("created"));
						Date date2 = DateUtil.dateFormat.parse(jsonObject2.getString("created"));
						return DateUtil.compare_date(date1, date2);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				return 0;
			}
		});
		return Result.csuccess(list);
	}
}
